package org.example.web.controller;

import org.example.biz.GlobalBiz;
import org.example.oa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller("globalController")
public class GlobalController {

    @Autowired
    private GlobalBiz globalBiz;

    //跳转到登陆界面login.jsp
    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    //登录
    @RequestMapping("/login")
    public String login(HttpSession httpSession, @RequestParam String sn, @RequestParam String password){
        Employee employee = globalBiz.login(sn,password);
        //登陆失败就重新登陆
        if(employee == null){
            return "redirect:to_login";
        }
        //登陆成功，保存到session中，重定向到self方法
        httpSession.setAttribute("employee", employee);
        return "redirect:self";
    }

    //跳转到个人信息界面self.jsp
    @RequestMapping("/self")
    public String self(){
        return "self";
    }

    //退出登录
    @RequestMapping("/quit")
    public String quit(HttpSession httpSession){
        httpSession.setAttribute("employee", null);
        return "redirect:to_login";
    }

    //跳转到修改密码界面change_password.jsp
    @RequestMapping("/to_change_password")
    public String toChangePassword(){
        return "change_password";
    }

    //修改密码
    @RequestMapping("/change_password")
    public String change(HttpSession httpSession, @RequestParam String old, @RequestParam String new1, @RequestParam String new2){

        Employee employee = (Employee) httpSession.getAttribute("employee");
        //旧密码输入正确且前后两次输入新密码相同，修改成功跳转到个人界面
        if(employee.getPassword().equals(old) && new1.equals(new2)){
            employee.setPassword(new1);
            globalBiz.changePassword(employee);
            return "redirect:self";
        }
        //修改失败跳转到修改密码界面
        return "redirect:to_change_password";
    }
}
